package com.yunhe.authority.service.authority.impl;

import com.yunhe.authority.domain.authority.Firm;
import com.yunhe.authority.domain.authority.FirmStaticData;
import com.yunhe.authority.repository.authority.FirmStaticDataRepository;
import com.yunhe.authority.service.authority.FirmService;
import com.yunhe.authority.service.authority.FirmStaticDataService;
import com.yunhe.common.exception.ObjectNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @ClassName FirmStaticDataServiceImpl
 * @Description 单位静态数据Service
 * @Author HeTao
 * @Date 2020/11/2 11:32
 * @Version 1.0
 **/
@Service
public class FirmStaticDataServiceImpl implements FirmStaticDataService {

    @Autowired
    private FirmStaticDataRepository firmStaticDataRepository;

    @Autowired
    private FirmService firmService;

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public FirmStaticData createFirmStaticData(FirmStaticData firmStaticData, Long firmId) throws ObjectNotFoundException {
        firmStaticData.setId(null);
        FirmStaticData newFirmStaticData = firmStaticDataRepository.save(firmStaticData);
        //与单位进行绑定
        if(firmId != null){
            bindFirm(newFirmStaticData, firmId);
        }
        return newFirmStaticData;
    }

    @Override
    public FirmStaticData updateFirmStaticData(FirmStaticData firmStaticData, Long firmId) throws ObjectNotFoundException {
        boolean exists = firmStaticDataRepository.existsById(firmStaticData.getId());
        if (!exists) {
            throw new ObjectNotFoundException("找不到单位Id为" + firmId + "的单位静态数据");
        }
        FirmStaticData newFirmStaticData = firmStaticDataRepository.save(firmStaticData);
        //与单位进行绑定
        if(firmId != null){
            bindFirm(newFirmStaticData, firmId);
        }
        return newFirmStaticData;
    }

    @Override
    public FirmStaticData updateFirmStaticData(Long id, Long firmId, Double area, Double taxes, Long ssCompNum
            , Long intendSsCompNum, Long electromechanicalCompNum, Long packingCompNum, Long materialsCompNum
            , Long automotiveCompNum, Long foodCompNum, Long otherCompNum, Long industryNum, Long compNumStaffAbove0
            , Long compNumStaffAbove50, Long compNumStaffAbove100, Long compNumStaffAbove150, Long allCompNum, Long signedCompNum
            , Long investmentCompNum, Long interestSignCompNum, Double annualOutputValue, Long allOfficeNum, Long solvingEmployment
            , Long regulatoryEquipmentNum, Double alarmProcessingRate, Long monitoringEquipmentNum, Double equipmentIntactRate
            , Long inspectionsNum, Long mediumAlarmNum, Double mediumAlarmProcessingRate, Long heaviestAlarmNum
            , Double heaviestAlarmProcessingRate, Long legalFinancialServiceNum, Long warehousingLogisticsServiceNum
            , Long policyDeclarationServiceNum, Long sharedCommunityServiceNum, Long financeServiceNum, Long trainingServiceNum
            , Long otherServiceNum, String[] energyCompListMonth, String[] energyCompListYear, String[] annualOutputValueStationList
            , String[] compNumStationList, String[] solvingEmploymentStationList) throws ObjectNotFoundException {
        FirmStaticData firmStaticData = findByFirmStaticDataId(id);
        if(firmStaticData == null){
            throw new ObjectNotFoundException("对应单位代码为"+ firmId +"的静态数据不存在");
        }
        Optional.ofNullable(area).ifPresent(firmStaticData::setArea);
        Optional.ofNullable(taxes).ifPresent(firmStaticData::setTaxes);
        Optional.ofNullable(ssCompNum).ifPresent(firmStaticData::setSsCompNum);
        Optional.ofNullable(intendSsCompNum).ifPresent(firmStaticData::setIntendSsCompNum);
        Optional.ofNullable(electromechanicalCompNum).ifPresent(firmStaticData::setElectromechanicalCompNum);
        Optional.ofNullable(packingCompNum).ifPresent(firmStaticData::setPackingCompNum);
        Optional.ofNullable(materialsCompNum).ifPresent(firmStaticData::setMaterialsCompNum);
        Optional.ofNullable(automotiveCompNum).ifPresent(firmStaticData::setAutomotiveCompNum);
        Optional.ofNullable(foodCompNum).ifPresent(firmStaticData::setFoodCompNum);
        Optional.ofNullable(otherCompNum).ifPresent(firmStaticData::setOtherCompNum);
        Optional.ofNullable(industryNum).ifPresent(firmStaticData::setIndustryNum);
        Optional.ofNullable(compNumStaffAbove0).ifPresent(firmStaticData::setCompNumStaffAbove0);
        Optional.ofNullable(compNumStaffAbove50).ifPresent(firmStaticData::setCompNumStaffAbove50);
        Optional.ofNullable(compNumStaffAbove100).ifPresent(firmStaticData::setCompNumStaffAbove100);
        Optional.ofNullable(compNumStaffAbove150).ifPresent(firmStaticData::setCompNumStaffAbove150);
        Optional.ofNullable(allCompNum).ifPresent(firmStaticData::setAllCompNum);
        Optional.ofNullable(signedCompNum).ifPresent(firmStaticData::setSignedCompNum);
        Optional.ofNullable(investmentCompNum).ifPresent(firmStaticData::setInvestmentCompNum);
        Optional.ofNullable(interestSignCompNum).ifPresent(firmStaticData::setInterestSignCompNum);
        Optional.ofNullable(annualOutputValue).ifPresent(firmStaticData::setAnnualOutputValue);
        Optional.ofNullable(allOfficeNum).ifPresent(firmStaticData::setAllOfficeNum);
        Optional.ofNullable(solvingEmployment).ifPresent(firmStaticData::setSolvingEmployment);
        Optional.ofNullable(regulatoryEquipmentNum).ifPresent(firmStaticData::setRegulatoryEquipmentNum);
        Optional.ofNullable(alarmProcessingRate).ifPresent(firmStaticData::setAlarmProcessingRate);
        Optional.ofNullable(monitoringEquipmentNum).ifPresent(firmStaticData::setMonitoringEquipmentNum);
        Optional.ofNullable(equipmentIntactRate).ifPresent(firmStaticData::setEquipmentIntactRate);
        Optional.ofNullable(inspectionsNum).ifPresent(firmStaticData::setInspectionsNum);
        Optional.ofNullable(mediumAlarmNum).ifPresent(firmStaticData::setMediumAlarmNum);
        Optional.ofNullable(mediumAlarmProcessingRate).ifPresent(firmStaticData::setMediumAlarmProcessingRate);
        Optional.ofNullable(heaviestAlarmNum).ifPresent(firmStaticData::setHeaviestAlarmNum);
        Optional.ofNullable(heaviestAlarmProcessingRate).ifPresent(firmStaticData::setHeaviestAlarmProcessingRate);
        Optional.ofNullable(legalFinancialServiceNum).ifPresent(firmStaticData::setLegalFinancialServiceNum);
        Optional.ofNullable(warehousingLogisticsServiceNum).ifPresent(firmStaticData::setWarehousingLogisticsServiceNum);
        Optional.ofNullable(policyDeclarationServiceNum).ifPresent(firmStaticData::setPolicyDeclarationServiceNum);
        Optional.ofNullable(sharedCommunityServiceNum).ifPresent(firmStaticData::setSharedCommunityServiceNum);
        Optional.ofNullable(financeServiceNum).ifPresent(firmStaticData::setFinanceServiceNum);
        Optional.ofNullable(trainingServiceNum).ifPresent(firmStaticData::setTrainingServiceNum);
        Optional.ofNullable(otherServiceNum).ifPresent(firmStaticData::setOtherServiceNum);
        Optional.ofNullable(energyCompListMonth).ifPresent(firmStaticData::setEnergyCompListMonth);
        Optional.ofNullable(energyCompListYear).ifPresent(firmStaticData::setEnergyCompListYear);
        Optional.ofNullable(annualOutputValueStationList).ifPresent(firmStaticData::setAnnualOutputValueStationList);
        Optional.ofNullable(compNumStationList).ifPresent(firmStaticData::setCompNumStationList);
        Optional.ofNullable(solvingEmploymentStationList).ifPresent(firmStaticData::setSolvingEmploymentStationList);
        FirmStaticData newFirmStaticData = firmStaticDataRepository.save(firmStaticData);
        //与单位进行绑定
        if(firmId != null){
            bindFirm(newFirmStaticData, firmId);
        }
        return newFirmStaticData;
    }

    @Override
    public FirmStaticData findByFirmId(Long firmId) {
        FirmStaticData firmStaticData = firmStaticDataRepository.findByFirmId(firmId);
        return firmStaticData;
    }

    private FirmStaticData findByFirmStaticDataId(Long id) {
        Optional<FirmStaticData> firmStaticDataOpt = firmStaticDataRepository.findById(id);
        FirmStaticData firmStaticData = null;
        if(firmStaticDataOpt.isPresent()){
            firmStaticData = firmStaticDataOpt.get();
        }
        return firmStaticData;
    }

    private void bindFirm(FirmStaticData newFirmStaticData, Long firmId) throws ObjectNotFoundException {
        //先判断当前单位是否已经绑定静态数据 并且与当前的id 是否一致
        Firm firm = firmService.findByFirmId(firmId);
        if(firm == null){
            throw new ObjectNotFoundException("单位Id为"+firmId+"的单位不存在");
        }
        Long fsdId = firmStaticDataRepository.findBindStaticDataFirmByFrimId(firm.getId());
        if(fsdId != null ){
            firmService.unbindFirmStaticData(fsdId,firm.getId());
        }
        firmService.bindFirmStaticData(newFirmStaticData.getId(),firm.getId());
    }
}
